// components/list-refresh/index.js
import {
  loadStatus,
  dataStatus
} from '../../utils/const'

const {
  globalData
} = getApp();

Component({
  externalClasses: ["custom-class", "item-node-class", "list-item-class"],
  options: {
    pureDataPattern: /^_/, // 指定所有 _ 开头的数据字段为纯数据字段
  },
  /**
   * 组件的属性列表
   */
  properties: {
    url: {
      type: String,
      value: "",
    },
    requestFun: {
      type: Function,
      value: null,
    },
    params: {
      type: Object,
      value: {},
    },
    imageFields: {
      type: Array,
      value: [],
    },
    lowerThreshold: {
      type: Number,
      value: 110,
    },
    // 是否组件创建完毕立即请求数据
    immediately: {
      type: Boolean,
      value: true,
    },
  },
  observers: {
    'params.**': function() {
      this.refresh();
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    visibleToTop: false,
    pageList: [], // 分页列表
    page: 1,
    totalPages: 0,
    loadStatus,
    dataStatus,
    curLoadStatus: loadStatus.none,
    curDataStatus: dataStatus.none,
    _pageSize: 10,
    _nums: 0,
  },
  /**
   * 组件的方法列表
   */
  methods: {
    refresh() {
      this.requestData();
    },
    lower() {
      const {
        page,
        totalPages
      } = this.data;
      if (totalPages > 0 && page >= totalPages) {
        return;
      }
      this.requestData(page + 1);
      this.setData({
        curLoadStatus: loadStatus.loading,
      });
    },
    scroll(event) {
      if (this.timer) {
        return;
      }
      this.timer = setTimeout(() => {
        this.checkScrollTop(event.detail.scr)
      })
    },
    checkScrollTop(top) {
      console.log(top);
    },
    resetData() {
      this.setData({
        pageList: [],
        curLoadStatus: loadStatus.refresh,
        curDataStatus: dataStatus.none,
      });
    },
    requestData(page = 1) {
      const {
        url,
        params,
        _pageSize,
        imageFields
      } = this.data;
      if (this.data.loading) {
        return;
      }

      if (this.data.curLoadStatus === loadStatus.loading) {
        return;
      }

      const requestParams = {
        page,
        pageSize: _pageSize,
        ...params,
      }
      const resultPromise = this.data.requestFun ? this.data.requestFun(requestParams) : wx.ajax
        .post(url, requestParams);
      resultPromise.then(({
          result
        }) => {
          let data = result || {};
          let setData = {
            curLoadStatus: loadStatus.none,
            curDataStatus: dataStatus.none,
            page: data.page,
            totalPages: data.totalPages,
            _nums: data.nums,
          };

          if (data.page === data.totalPages) {
            setData.curDataStatus = dataStatus.end;
          }

          data.pageList.forEach((item) => {
            imageFields.forEach((field) => {
              item[field] = globalData.imageUrl + item[field];
            });
          });

          if (data.page === 1) {
            setData.curDataStatus =
              data.pageList.length > 0 ?
              setData.curDataStatus :
              dataStatus.noData;
            setData.pageList = [data.pageList];
          } else {
            setData[`pageList[${data.page - 1}]`] = data.pageList;
          }

          this.setData(setData);
        })
        .catch(() => {
          this.setData({
            curLoadStatus: loadStatus.none,
          });
        });
    },
    itemClickHandle(event) {
      this.triggerEvent("item-click", event.detail);
    },
    handleItemEvent(event) {
        this.triggerEvent('item-event', event.detail)
    }
  },
  attached() {},
  ready() {
    if (this.properties.immediately) {
      this.setData({
        curLoadStatus: loadStatus.refresh,
      });
    }
  },
  detached() {},
});